package com.foodmarket.controller;


import com.foodmarket.pojo.Orders;
import com.foodmarket.pojo.Result;
import com.foodmarket.service.OrderService;
import com.foodmarket.pojo.buyerOrders;

import com.foodmarket.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    //查询
    @GetMapping("/allOrderInfo")
    public Result<List<Orders>> allOrderInfo(){
        //根据用户id查询用户
        Map<String,Object> map=ThreadLocalUtil.get();
        System.out.println(map);

        int SellerId=(int) map.get("id");

        List<Orders> orders=orderService.findAllOrder(SellerId);
        return Result.success(orders);
    }

    @GetMapping("/buyerOrders")
    public Result<List<buyerOrders>> findBuyerOrders(){

        List<buyerOrders> buyerOrders=orderService.findBuyersOrder();
        return Result.success(buyerOrders);


    }

    //增加（买家）
    @PostMapping("addOrder")
    public Result addOrder(@RequestBody Orders orders)
    {
        orderService.add(orders);
        return Result.success();
    }

    //删除（卖家）
    @DeleteMapping("/deleteOrder")//删除
    public Result deleteById(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer buyerId = (Integer) map.get("id");
        orderService.deleteByBuyerId(buyerId);
        return Result.success();
    }

}